﻿using System;
using System.Collections.Generic;
using System.Text;

using Gx.Dal.Employee;//数据
using Gx.Model.Entity;//实体
using Gx.Common;//通用类
using Gx.Bll.Shared;
using Gx.Model.Enums;
using System.Linq;

namespace Gx.Bll.Employee
{
    public class G_PurchaseOrderGoodsInfoBll
    {
        public bool AddG_PurchaseOrderGoodsInfo(G_PurchaseOrderGoodsInfo model)
        {
            using (G_PurchaseOrderGoodsInfoDal dal = new G_PurchaseOrderGoodsInfoDal())
            {
                return dal.AddG_PurchaseOrderGoodsInfo(model);
            }
        }

        public bool DelG_PurchaseOrderGoodsInfo(int stockInGoodsId)
        {
            using (G_PurchaseOrderGoodsInfoDal dal = new G_PurchaseOrderGoodsInfoDal())
            {
                return dal.DelG_PurchaseOrderGoodsInfo(stockInGoodsId);
            }
        }

        public bool EditG_PurchaseOrderGoodsInfo(G_PurchaseOrderGoodsInfo model)
        {
            using (G_PurchaseOrderGoodsInfoDal dal = new G_PurchaseOrderGoodsInfoDal())
            {
                return dal.EditG_PurchaseOrderGoodsInfo(model);
            }
        }

        public List<G_PurchaseOrderGoodsInfo> GetG_PurchaseOrderGoodsInfoList(int PurchaseOrderId)
        {
            using (G_PurchaseOrderGoodsInfoDal dal = new G_PurchaseOrderGoodsInfoDal())
            {
                StringBuilder strWhere = new StringBuilder();
                strWhere.AppendFormat(" AND PurchaseOrderId={0}", PurchaseOrderId);
                List<G_PurchaseOrderGoodsInfo> list = dal.GetG_PurchaseOrderGoodsInfoList(strWhere.ToString());

                List<M_DictInfo> dictList = new M_DictInfoBll().GetDictInfoList(new List<int>() { (int)DictEnmus.计量单位 });
                List<G_GoodsInfo> goodsList = new G_GoodsInfoBll().GetG_GoodsInfoList(list.Select(x => x.GoodsId).ToList());
                foreach (var item in list)
                {
                    item.StrUnitValue = dictList.Exists(x => x.DictValue == item.UnitValue) ? dictList.Find(x => x.DictValue == item.UnitValue).DictName : "单位不存在或已被删除";
                    item.StrNeedNum = item.NeedNum.ToStringf2();
                    G_GoodsInfo goods = goodsList.Find(x => x.GoodsId == item.GoodsId);
                    if (goods != null)
                    {
                        item.GoodsName = goods.GoodsName;
                        item.Specs = goods.Specs;
                        item.TypeName = goods.TypeName;
                        item.ColorName = goods.ColorName;
                    }
                }
                return list;
            }
        }

        private List<G_PurchaseOrderGoodsInfo> GetG_PurchaseOrderGoodsInfoList(string strWhere)
        {
            using (G_PurchaseOrderGoodsInfoDal dal = new G_PurchaseOrderGoodsInfoDal())
            {
                return dal.GetG_PurchaseOrderGoodsInfoList(strWhere);
            }
        }

        public List<G_PurchaseOrderGoodsInfo> GetG_PurchaseOrderGoodsInfoList(
        int pageIndex, int pageSize, out int totalCount)
        {
            using (G_PurchaseOrderGoodsInfoDal dal = new G_PurchaseOrderGoodsInfoDal())
            {
                int PageIndex = (pageIndex - 1) * pageSize + 1;
                int PageSize = pageIndex * pageSize;
                StringBuilder strWhere = new StringBuilder();
                return dal.GetG_PurchaseOrderGoodsInfoList(strWhere.ToString(), PageIndex, PageSize, out totalCount);
            }
        }

        private int GetG_PurchaseOrderGoodsInfoCount(string strWhere)
        {
            using (G_PurchaseOrderGoodsInfoDal dal = new G_PurchaseOrderGoodsInfoDal())
            {
                return dal.GetG_PurchaseOrderGoodsInfoCount(strWhere);
            }
        }
        public G_PurchaseOrderGoodsInfo GetG_PurchaseOrderGoodsInfoModel(int stockInGoodsId)
        {
            using (G_PurchaseOrderGoodsInfoDal dal = new G_PurchaseOrderGoodsInfoDal())
            {
                return dal.GetG_PurchaseOrderGoodsInfoModel(stockInGoodsId);
            }
        }
    }
}
